package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.lock.CharSkillReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_AddSkill;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Skills;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AutoAddSkillTable {
  private static final Log _log = LogFactory.getLog(AutoAddSkillTable.class);
  
  private static HashMap<Integer, HashMap<Integer, int[]>> _array = new HashMap<>();
  
  private static AutoAddSkillTable _instance;
  
  public static AutoAddSkillTable get() {
    if (_instance == null)
      _instance = new AutoAddSkillTable(); 
    return _instance;
  }
  
  private AutoAddSkillTable() {
    load();
    _log.info("升级自学技能->" + _array.size());
    if (_array.size() <= 0) {
      _array.clear();
      _array = null;
    } 
  }
  
  private void load() {
    Connection cn = null;
    Statement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.createStatement();
      rs = ps.executeQuery("SELECT * FROM w_自动学习技能");
      while (rs.next()) {
        int level = rs.getInt("Level");
        int[] skills = getArray(rs.getString("Skill"));
        boolean r = rs.getBoolean("Royal");
        boolean k = rs.getBoolean("Knight");
        boolean m = rs.getBoolean("Mage");
        boolean e = rs.getBoolean("Elf");
        boolean d = rs.getBoolean("Darkelf");
        boolean g = rs.getBoolean("DragonKnight");
        boolean i = rs.getBoolean("Illusionist");
        int type = 0;
        if (r) {
          type = 0;
        } else if (k) {
          type = 1;
        } else if (m) {
          type = 3;
        } else if (e) {
          type = 2;
        } else if (d) {
          type = 4;
        } else if (g) {
          type = 5;
        } else if (i) {
          type = 6;
        } 
        HashMap<Integer, int[]> _type = _array.get(Integer.valueOf(type));
        if (_type == null)
          _type = (HashMap)new HashMap<>(); 
        _type.put(Integer.valueOf(level), skills);
        _array.put(Integer.valueOf(type), _type);
      } 
    } catch (SQLException e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private static int[] getArray(String s) {
    if (s == null || s.equals("") || s.equals("0"))
      return null; 
    StringTokenizer st = new StringTokenizer(s, ",");
    int iSize = st.countTokens();
    String sTemp = null;
    int[] iReturn = new int[iSize];
    int i = 0;
    while (i < iSize) {
      sTemp = st.nextToken();
      iReturn[i] = Integer.parseInt(sTemp);
      i++;
    } 
    return iReturn;
  }
  
  public void forAutoAddSkill(L1PcInstance pc) {
    if (_array == null)
      return; 
    int classtype = pc.getType();
    if (!_array.containsKey(Integer.valueOf(classtype)))
      return; 
    HashMap<Integer, int[]> _classtype = _array.get(Integer.valueOf(classtype));
    int level = pc.getLevel();
    Iterator<Integer> iterator = _classtype.keySet().iterator();
    while (iterator.hasNext()) {
      Integer key = iterator.next();
      if (key.intValue() > level)
        continue; 
      int[] skills = _classtype.get(key);
      int i = 0;
      int skillId = 0;
      while (i < skills.length) {
        skillId = skills[i];
        if (!pc.isSkillMastery(skillId)) {
          pc.sendPackets((ServerBasePacket)new S_AddSkill(pc, skillId));
          S_SkillSound sound = new S_SkillSound(pc.getId(), 227);
          pc.sendPacketsX8((ServerBasePacket)sound);
          L1Skills skill = SkillsTable.get().getTemplate(skillId);
          String skillName = skill.getName();
          CharSkillReading.get().spellMastery(pc.getId(), skillId, skillName, 0, 0);
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("自动学习 ( " + skillName + " ) 技能"));
        } 
        i++;
      } 
    } 
  }
}
